<?php

namespace adminApi\modules\product\controllers;

use adminApi\models\db\SpuMaterials;
use adminApi\models\db\Spus;
use common\components\fast_api\Controller;
use Yii;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;

/**
 * 素材管理
 */
class MaterialsController extends Controller
{
    /**
     * 素材列表
     */
    public function actionList($spuId)
    {
        $materials = SpuMaterials::find()
            ->with(['file'])
            ->andWhere(['spu_id' => $spuId])
            ->orderBy(['material_sort' => SORT_ASC])
            ->all();
        $list = [];
        foreach ($materials as $k => $v) {
            /** @var SpuMaterials $v */
            $m['id'] = $v->id;
            $m['material_type'] = $v->material_type;
            $m['_material_type'] = $v::getMaterialTypeDesc()[$v->material_type];
            $m['material_text'] = $v->material_text;
            $m['material_file_id'] = $v->material_file_id;
            $m['_material_file'] = $v->file;
            $m['material_sort'] = $v->material_sort;
            $list[] = $m;
        }
        return $this->apiSuccess([
            'list' => $list,
        ]);
    }

    /**
     * 素材编辑
     * material_type 1 文本 2 图片 3 视频
     */
    public function actionEdit($spuId, $listForm)
    {
        $spu = Spus::findOne($spuId);
        if (!$spu) {
            return $this->apiError('商品不存在');
        }
        $listForm = json_decode($listForm, true);
        $transaction = Yii::$app->db->beginTransaction();
        try {
            $oldMaterials = SpuMaterials::find()
                ->andWhere(['spu_id' => $spuId])
                ->all();
            $oldMaterialIds = $listFormIds = [];
            foreach ($oldMaterials as $item) {
                $oldMaterialIds[] = $item->id;
            }
            foreach ($listForm as $item) {
                $listFormIds[] = $item['id'];
            }
            $adds = $updates = [];
            $deleteIds = array_diff($oldMaterialIds, $listFormIds);
            SpuMaterials::deleteAll(['id' => $deleteIds]);
            foreach ($listForm as $item) {
                if (!$item['id']) {
                    $adds[] = $item;
                } else {
                    if ($item['isUpdated']) {
                        $updates[] = $item;
                    }
                }
            }
            foreach ($adds as $item) {
                $material = new SpuMaterials();
                $material->spu_id = $spuId;
                $material->material_type = $item['material_type'];
                $material->material_text = $item['material_text'];
                $material->material_file_id = $item['material_file_id'];
                $material->material_sort = $item['material_sort'];
                $material->save();
            }
            foreach ($updates as $k => $item) {
                $material = SpuMaterials::findOne($item['id']);
                if (!$material) {
                    return $this->apiError('素材不存在');
                }
                $material->material_type = $item['material_type'];
                $material->material_text = $item['material_text'];
                $material->material_file_id = $item['material_file_id'];
                $material->material_sort = $item['material_sort'];
                $material->save();
            }
            $transaction->commit();
            return $this->apiSuccess();
        }catch (\Exception $e) {
            $transaction->rollBack();
            return $this->apiError($e->getMessage());
        }
    }
}
